home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / BackBounce / Source / BounceView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.7 KB  |  145 lines

  1.  
  2. /* Generated by Interface Builder */
  3. /* This is certainly a grody file.  But it works.  So there.  I have a better one
  4.  * around, which is Interface Builder custom palette compatible.  Give me time. */
  5.  
  6. #import "BounceView.h"
  7. #import <appkit/Window.h>
  8. #import <appkit/Application.h>
  9. #import <dpsclient/wraps.h>
  10. #import "Animator.h"
  11. #import <appkit/publicWraps.h>
  12. #import <strings.h>
  13. #import <appkit/Bitmap.h>
  14.  
  15. @implementation BounceView
  16.  
  17. #define RANDINT( n) ((random() % (n+1))+1)
  18.  
  19. extern srandom(int);
  20. extern long random();
  21. extern long time( long *);
  22.  
  23. + newFrame:(NXRect *)r
  24. {
  25.   int i;
  26.   self = [super newFrame:r];
  27.   [[NXApp appIcon] setContentView:self];
  28.   for( i=0; i<4; i++)            // get random velocities.
  29.     vel[ i]=(RANDINT( 1) ? 1 : -1)*(1+minVel+RANDINT( (maxVel-minVel)/2));
  30.   pos[ 0][ 0]=pos[ 0][ 2]=bounds.size.width/2;
  31.   pos[ 0][ 1]=pos[ 0][ 3]=bounds.size.height/2;
  32.   for( i=0; i<numEdges; i++)
  33.     [self moveLine];
  34.   srandom( time( 0));
  35.   return self;
  36. }
  37.  
  38. - free
  39. {
  40.   [animator stop:self];
  41.   [animator setTarget:nil];
  42.   return [super free];
  43. }
  44.  
  45. - setAnimator:anObject
  46. {
  47.   if( animator==anObject)            // nip loops in the bud.
  48.     return self;
  49.   animator=anObject;
  50.   [animator setTarget:self];
  51.   [animator setTiming:0.3];
  52.   [animator setThreshold:NX_MODALRESPTHRESHOLD];    // this makes it run smoother. why?
  53.   return self;
  54. }
  55.  
  56. - animate:sender
  57. {
  58.   [self lockFocus];
  59.   PSsetgray( NX_DKGRAY);
  60.   PSnewpath();
  61.   PSmoveto( bounds.origin.x+pos[ numEdges-1][ 0], bounds.origin.y+pos[ numEdges-1][ 1]);
  62.   PSlineto( bounds.origin.x+pos[ numEdges-1][ 2], bounds.origin.y+pos[ numEdges-1][ 3]);
  63.   PSstroke();
  64.   [self moveLine];
  65.   PSsetgray( NX_BLACK);
  66.   PSnewpath();
  67.   PSmoveto( bounds.origin.x+pos[ 0][ 0], bounds.origin.y+pos[ 0][ 1]);
  68.   PSlineto( bounds.origin.x+pos[ 0][ 2], bounds.origin.y+pos[ 0][ 3]);
  69.   PSstroke();
  70.   [self unlockFocus];
  71.   [[self window] flushWindow];
  72.   NXPing();                // This makes it run smoother - else code is output
  73.                       // in larger chunks.
  74.   return self;
  75. }
  76.  
  77. - animator
  78. {
  79.   return animator;
  80. }
  81.  
  82. - drawSelf:(NXRect *)r :(int)count
  83. {
  84.   int i;
  85.   PSsetgray( NX_DKGRAY);
  86.   NXRectFill( &bounds);
  87.   PSsetgray( NX_BLACK);
  88.   PSnewpath();
  89.   for( i=0; i<numEdges; i++)        // Not bad.  Could be a userpath, though.
  90.     {
  91.       PSmoveto( bounds.origin.x+pos[ i][ 0], bounds.origin.y+pos[ i][ 1]);
  92.       PSlineto( bounds.origin.x+pos[ i][ 2], bounds.origin.y+pos[ i][ 3]);
  93.     }
  94.   PSstroke();
  95.   [[self window] flushWindow];
  96.   NXPing();                // Do we need to be smooth here?  Maybe not.
  97.                       // but who cares, as we only redraw seldom.
  98.   return self;
  99. }
  100.  
  101. - moveLine            // This is stupid.  Anyone have a better way?  I once did,
  102.                 // but it was non-intuitive.  There must be some generic
  103.                 // method.  If so, please tell me.
  104. {
  105.     int i;
  106.     bcopy( pos, &pos[ 1], sizeof( short)*4*(numEdges-1));
  107.     for( i=0; i<4; i++)
  108.       pos[ 0][ i]+=vel[ i];
  109.     if( pos[ 0][ 0]>bounds.size.width || pos[ 0][ 0]<0)
  110.       {
  111.         if( pos[ 0][ 0]<0)
  112.       pos[ 0][ 0]=0;
  113.     else
  114.       pos[ 0][ 0]=bounds.size.width-1;
  115.     vel[ 0]=(vel[ 0]<0 ? 1 : -1)*(minVel+RANDINT( maxVel-minVel));
  116.       }
  117.     if( pos[ 0][ 1]>bounds.size.height || pos[ 0][ 1]<0)
  118.       {
  119.         if( pos[ 0][ 1]<0)
  120.       pos[ 0][ 1]=0;
  121.     else
  122.       pos[ 0][ 1]=bounds.size.height-1;
  123.     vel[ 1]=(vel[ 1]<0 ? 1 : -1)*(minVel+RANDINT( maxVel-minVel));
  124.       }
  125.     if( pos[ 0][ 2]>bounds.size.width || pos[ 0][ 2]<0)
  126.       {
  127.         if( pos[ 0][ 2]<0)
  128.       pos[ 0][ 2]=0;
  129.     else
  130.       pos[ 0][ 2]=bounds.size.width-1;
  131.     vel[ 2]=(vel[ 2]<0 ? 1 : -1)*(minVel+RANDINT( maxVel-minVel));
  132.       }
  133.     if( pos[ 0][ 3]>bounds.size.height || pos[ 0][ 3]<0)
  134.       {
  135.         if( pos[ 0][ 3]<0)
  136.       pos[ 0][ 3]=0;
  137.     else
  138.       pos[ 0][ 3]=bounds.size.height-1;
  139.     vel[ 3]=(vel[ 3]<0 ? 1 : -1)*(minVel+RANDINT( maxVel-minVel));
  140.       }
  141.     return self;
  142. }
  143.  
  144. @end
  145.